package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public Page<Setmeal> findByPage(String name, Integer pageNum, Integer pageSize) {
        //1. 根据name模糊分页查询套餐信息(从setmeal表)
        //1-1 设置分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);

        //1-2 设置查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);

        //1-3 进行查询
        page = setmealMapper.selectPage(page, wrapper);

        //2 遍历
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //3 根据套餐categoryId去category表中查询对应信息
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
            }
        }

        return page;
    }

    @Override
    public void save(Setmeal setmeal) {
        //1. 将基本信息保存到setmeal表
        setmealMapper.insert(setmeal);

        //2. 遍历前端传入的菜品信息
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                //3. 设置上套餐id
                setmealDish.setSetmealId(setmeal.getId());

                //4. 将套餐菜品保存到setmeal_dish表
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        //1. 删除套餐之前,要先去判断当前要删除的这一批套餐是否有在售状态,一旦有一个在售,整个删除失败
        //select count(0) from setmeal where status = 1 and id in #{ids}
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getStatus, 1)//状态为1(在售)
                .in(Setmeal::getId, ids);
        Integer num = setmealMapper.selectCount(wrapper);
        if (num > 0) {
            throw new CustomException("当前套餐处于在售状态,无法删除");
        }

        //2. 删除套餐的时候,要将中间表的当前套餐相关数据一并删除
        //2-1 删除中间表
        //delete from setmeal_dish where setmeal_id in #{ids}
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper1);

        //2-2 删除套餐表
        //delete from setmeal where status = 0 and id in #{ids}
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getStatus, 0)//状态为0(不在售)
                .in(Setmeal::getId, ids);
        setmealMapper.delete(wrapper2);
    }

    @Override
    public List<Setmeal> findByCategoryId(Long categoryId, Integer status) {

        //select * from setmeal where category_id=#{} and status = #{}
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status);

        return setmealMapper.selectList(wrapper);
    }
}